package com.example.communist_app_api.controller.uniapp;


import com.example.communist_app_api.db.pojoDto.*;
import com.example.communist_app_api.service.*;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.ArticleIdReq;
import com.example.communist_app_api.vo.uniapp.req.ArticleTypeReq;
import com.example.communist_app_api.vo.uniapp.req.titleReq;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/uniApp/article/")
@Api("文章controller控制器")
public class UniAppArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleTypeService articleTypeService;

    @Autowired
    private CartService cartService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LikeService likeService;

    @Autowired
    private UserSoncommentService userSoncommentService;

    @Autowired
    private UserCommentLikeService userCommentLikeService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserOrganizationService userOrganizationService;

    @Autowired
    private OrganizationService organizationService;


    //@PostMapping("/showArticle")
    @ApiOperation("查看全部文章")
    @GetMapping("/showArticle")
    public R showAllArticle(HttpServletRequest httpServletRequest)
    {
        //查看文章用户无需权限，全部放行，不用查看权限
        Integer userId = getUserIdByWeb(httpServletRequest);
        //判断用户所属组织
        int organizationId = userOrganizationService.getOrganizationIdByUserId(userId);
        //拿到用户组织id后需判断组织属于哪一等级
        String OrganizationLevel = organizationService.getOrganizationLevelById(organizationId);
        List<Article> articleList;
        if (Objects.equals(OrganizationLevel, "党总支"))
        {
            articleList = articleService.getAllArticle();
        }
        else {
            articleList = articleService.getArticleByParentId(organizationId);
        }
        if (articleList == null)
        {
            return R.error("文章为空");
        }
        else
            return R.ok("查询成功").put("articleList",articleList);

    }

    //@PostMapping("/showTopArticle")
    @ApiOperation("查询置顶文章")
    @GetMapping("/showTopArticle")
    public R showTopArticle(HttpServletRequest httpServletRequest){
        Integer userId = getUserIdByWeb(httpServletRequest);
        int organizationId = userOrganizationService.getOrganizationIdByUserId(userId);
        //拿到用户组织id后需判断组织属于哪一等级
        String OrganizationLevel = organizationService.getOrganizationLevelById(organizationId);
        if (Objects.equals(OrganizationLevel, "党总支"))
        {
            List<Article> list  = articleService.getAllTopArticle();
            if (list == null)
            {
                return R.error("文章为空");
            }
            else
                return R.ok("查询成功").put("list",list);
        }
        else
        {
            List<Article> list = articleService.getTopArticle(organizationId);
            if (list == null)
            {
                return R.error("文章为空");
            }
            else
                return R.ok("查询成功").put("list",list);
        }
    }

    @PostMapping("/showArticleTypeId")
    @ApiOperation("按文章分类查看文章")
    public  R showCategorizeArticles(@RequestBody ArticleTypeReq req,HttpServletRequest httpServletRequest)
    {
        Integer userId = getUserIdByWeb(httpServletRequest);
        int organizationId = userOrganizationService.getOrganizationIdByUserId(userId);
        //拿到用户组织id后需判断组织属于哪一等级
        String OrganizationLevel = organizationService.getOrganizationLevelById(organizationId);
        Integer articleTypeId = req.getArticleTypeId();
        if (Objects.equals(OrganizationLevel, "党总支"))
        {
            List<Article> articleList = articleService.getCategorizeArticles(articleTypeId);
            if (articleList == null){
                return R.error("文章为空");
            }
            else
                return R.ok("查找成功").put("articleList",articleList);
        }
        else
        {
            List<Article> articleList = articleService.getOrganizationIdCategorizeArticles(articleTypeId,organizationId);
            if (articleList == null){
                return R.error("文章为空");
            }
            else
                return R.ok("查找成功").put("articleList",articleList);
        }

    }

    @PostMapping("/showArticleDetail")
    @ApiOperation("查看文章详情")
    //@GetMapping("/showArticleDetail")
    public R showArticleDetail(@RequestBody ArticleIdReq req,HttpServletRequest httpServletRequest)
    {
        Integer id = req.getId();
        //增加阅读量
        Integer UserId = getUserIdByWeb(httpServletRequest);
        articleService.UpdateReading(id);
        //获取阅读量
        int reading = articleService.getReading(id);
        //文章内容
        Article article = articleService.getArticleById(id);
        //文章点赞数量
        int likeNum = likeService.getLikeNum(id);
        article.setLikeNum(likeNum);
        //文章收藏数量
        int cartNum = cartService.getCartNum(id);
        article.setCartNum(cartNum);
        //文章评论
        List<Comment> comments = commentService.getAllComment(id);
        List<Comment> list = new ArrayList<>();
        //评论点赞数组
        List likeList = new ArrayList();

        if (comments.size()!=0)
        {
            for (int i = 0;i < comments.size();i++)
        {
            Comment comment = new Comment();
            int commentId = comments.get(i).getId();
            List<UserSoncomment> userSoncomment = userSoncommentService.getUserComment(commentId);
            Integer userId  = comments.get(i).getUserId();
            Boolean like = userCommentLikeService.judgmentLike(UserId,commentId);
            likeList.add(like);

            for (int j = 0;j < userSoncomment.size();j++)
            {
                Integer userId1 =  userSoncomment.get(j).getUserId();
                User user = userService.getNamePhoto(userId1);
                UserSoncomment userSoncomment1 = new UserSoncomment();
                userSoncomment.get(j).setPhoto(user.getPhoto());
                userSoncomment.get(j).setName(user.getName());
                userSoncomment.get(j).setOrganizationName(userOrganizationService.selectOrganizationNameByUserId(userId1));
            }
            User user  = userService.getNamePhoto(userId);
            comment.setPhoto(user.getPhoto());
            comment.setName(user.getName());
            comment.setOrganizationName(userOrganizationService.selectOrganizationNameByUserId(userId));
            comment.setLike(like);
            comment.setId(comments.get(i).getId());
            comment.setUserId(comments.get(i).getUserId());
            comment.setArticleId(comments.get(i).getArticleId());
            comment.setAuditorId(comments.get(i).getAuditorId());
            comment.setContext(comments.get(i).getContext());
            comment.setLikeNum(comments.get(i).getLikeNum());
            comment.setUserSoncomments(userSoncomment);
            list.add(comment);
        }
        }

        //判断用户是否已点赞和收藏
        Boolean like = likeService.judgmentLike(UserId,id);
        Boolean cart = cartService.judgmentCart(UserId,id);
        System.out.println(list);


        return R.ok("成功").put("article",article).put("list",list).put("like",like).put("cart",cart).put("reading",reading).put("likeList",likeList);
    }


    @PostMapping("/SearchQueryArticles")
    @ApiOperation("搜索查询文章")
    public R SearchQueryArticles(@RequestBody titleReq req, HttpServletRequest httpServletRequest){
        Integer userId = getUserIdByWeb(httpServletRequest);
        int organizationId = userOrganizationService.getOrganizationIdByUserId(userId);
        //拿到用户组织id后需判断组织属于哪一等级
        String OrganizationLevel = organizationService.getOrganizationLevelById(organizationId);
        if (OrganizationLevel == "党总支"){
            List<Article> articles = articleService.SearchQueryArticles(req.getTitle());
            return R.ok().put("articles",articles);
        }
        else {
            List<Article> articles = articleService.SearchQueryOrganizationIdArticles(req.getTitle(),organizationId);
            return R.ok().put("articles",articles);
        }

    }


    private Integer getUserIdByWeb(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String id = claims.getSubject();
        int userId = Integer.parseInt(id);
        return userId;
    }
}
